<?php

/*
 * This file is part of the symfony package.
 * (c) Walter Seewald <walterseewald@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * sfWidgetFormInvoice represents a invoice widget.
 *
 * @package    symfony
 * @subpackage widget
 * @author     Walter Seewald <walterseewald@gmail.com>
 * @version    sfWidgetFormInvoice.class.php 1.0 2010-08-25 16:40:19 $
 */
class sfWidgetFormInvoice extends sfWidgetFormInput
{
  /**
   * Configures the current widget.
   *
   * Available options:
   *
   *  * talonario:    An string representing the invoice block number (4 chars)
   *  * factura:      An string representing the invoice number (8 chars)
   *  * can_be_empty: Whether the widget accept an empty value (false by default)
   *  * empty_values: An array of values to use for the empty value (empty string for talonario and factura by default)
   *
   * @param array $options     An array of options
   * @param array $attributes  An array of default HTML attributes
   *
   * @see sfWidgetForm
   */
  protected function configure($options = array(), $attributes = array())
  {
    $this->addOption('can_be_empty', false);
    $this->addOption('empty_values', array('talonario' => '', 'factura' => ''));
  }

  /**
   * @param  string $name        The element name
   * @param  string $value       The date displayed in this widget
   * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
   * @param  array  $errors      An array of errors for the field
   *
   * @return string An HTML tag string
   *
   * @see sfWidgetForm
   */
  public function render($name, $value = null, $attributes = array(), $errors = array())
  {
    // convert value to an array
	if(!empty($value)){
		if(!is_array($value)){
			$value = array_combine(array('talonario','factura'),explode('-',$value));
		}
	}

    $invoice = array();

	$invoiceN = $value;
    $invoice['%talonario%'] = $this->renderTalonarioWidget($name.'[talonario]', $invoiceN['talonario'], array(), array_merge($this->attributes, array('size' => 4, 'maxlength' => 4)));
    $invoice['%factura%'] = $this->renderFacturaWidget($name.'[factura]', $invoiceN['factura'], array('type' => 'text'), array_merge($this->attributes, array('size' => 8, 'maxlength' => 8)));

    return implode('-',$invoice);
  }

  /**
   * @param string $name
   * @param string $value
   * @param array $options
   * @param array $attributes
   * @return string rendered widget
   */
  protected function renderTalonarioWidget($name, $value, $options, $attributes)
  {
    $widget = new sfWidgetFormInputText($options, $attributes);
    return $widget->render($name, $value);
  }

  /**
   * @param string $name
   * @param string $value
   * @param array $options
   * @param array $attributes
   * @return string rendered widget
   */
  protected function renderFacturaWidget($name, $value, $options, $attributes)
  {
    $widget = new sfWidgetFormInputText($options, $attributes);
    return $widget->render($name, $value);
  }
}
